'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
    /**
     * 获取用户列表（支持筛选和分页）
     * GET /api/user/list
     */
    async getUserList() {
        const { ctx, service } = this;
        const { page = 1, pageSize = 10, role, status, keyword } = ctx.query;

        // 验证token和管理员权限
        const { token } = ctx.request.headers;
        const tokenData = service.auth.verifyToken(token);

        if (!tokenData) {
            ctx.body = {
                success: false,
                message: '未登录或token无效'
            };
            return;
        }

        if (tokenData.role !== 'admin') {
            ctx.body = {
                success: false,
                message: '无管理员权限'
            };
            return;
        }

        // 处理status参数：空字符串或undefined都视为不筛选
        let statusValue = undefined;
        if (status !== undefined && status !== '' && status !== null) {
            statusValue = parseInt(status);
        }

        const result = await service.user.getUserList({
            page: parseInt(page),
            pageSize: parseInt(pageSize),
            role: role || undefined,
            status: statusValue,
            keyword: keyword || undefined
        });

        ctx.body = result;
    }

    /**
     * 获取用户详情
     * GET /api/user/:id
     */
    async getUserDetail() {
        const { ctx, service } = this;
        const { id } = ctx.params;

        // 验证token和管理员权限
        const { token } = ctx.request.headers;
        const tokenData = service.auth.verifyToken(token);

        if (!tokenData) {
            ctx.body = {
                success: false,
                message: '未登录或token无效'
            };
            return;
        }

        if (tokenData.role !== 'admin') {
            ctx.body = {
                success: false,
                message: '无管理员权限'
            };
            return;
        }

        const result = await service.user.getUserDetail(parseInt(id));
        ctx.body = result;
    }

    /**
     * 更新用户状态（启用/禁用）
     * PUT /api/user/:id/status
     */
    async updateUserStatus() {
        const { ctx, service } = this;
        const { id } = ctx.params;
        const { status } = ctx.request.body;

        // 验证token和管理员权限
        const { token } = ctx.request.headers;
        const tokenData = service.auth.verifyToken(token);

        if (!tokenData) {
            ctx.body = {
                success: false,
                message: '未登录或token无效'
            };
            return;
        }

        if (tokenData.role !== 'admin') {
            ctx.body = {
                success: false,
                message: '无管理员权限'
            };
            return;
        }

        const result = await service.user.updateUserStatus(parseInt(id), status);
        ctx.body = result;
    }

    /**
     * 更新用户角色
     * PUT /api/user/:id/role
     */
    async updateUserRole() {
        const { ctx, service } = this;
        const { id } = ctx.params;
        const { role } = ctx.request.body;

        // 验证token和管理员权限
        const { token } = ctx.request.headers;
        const tokenData = service.auth.verifyToken(token);

        if (!tokenData) {
            ctx.body = {
                success: false,
                message: '未登录或token无效'
            };
            return;
        }

        if (tokenData.role !== 'admin') {
            ctx.body = {
                success: false,
                message: '无管理员权限'
            };
            return;
        }

        const result = await service.user.updateUserRole(parseInt(id), role);
        ctx.body = result;
    }

    /**
     * 删除用户
     * DELETE /api/user/:id
     */
    async deleteUser() {
        const { ctx, service } = this;
        const { id } = ctx.params;

        // 验证token和管理员权限
        const { token } = ctx.request.headers;
        const tokenData = service.auth.verifyToken(token);

        if (!tokenData) {
            ctx.body = {
                success: false,
                message: '未登录或token无效'
            };
            return;
        }

        if (tokenData.role !== 'admin') {
            ctx.body = {
                success: false,
                message: '无管理员权限'
            };
            return;
        }

        const result = await service.user.deleteUser(parseInt(id));
        ctx.body = result;
    }

    /**
     * 获取用户统计信息
     * GET /api/user/stats
     */
    async getUserStats() {
        const { ctx, service } = this;

        // 验证token和管理员权限
        const { token } = ctx.request.headers;
        const tokenData = service.auth.verifyToken(token);

        if (!tokenData) {
            ctx.body = {
                success: false,
                message: '未登录或token无效'
            };
            return;
        }

        if (tokenData.role !== 'admin') {
            ctx.body = {
                success: false,
                message: '无管理员权限'
            };
            return;
        }

        const result = await service.user.getUserStats();
        ctx.body = result;
    }
}

module.exports = UserController;

